package duppentity.dto.merchant;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import utils.JSONObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 将dupp返回的数据重新进行封装
 * Author: pengpeng@djcars.cn
 * Date: 2019/12/10 10:00
 */

public class BuMerchantCouponDtoUtil {
    public static List<BuMerchantCouponDto> getBuMerchantCouponDtoList(JsonObject data) {
        List<BuMerchantCouponDto> buMerchantCouponDtoList = null;
        if (data != null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    if (list != null && list.size() > 0) {
                        buMerchantCouponDtoList = new ArrayList<>();
                        for (Integer i = 0; i < list.size(); i++) {
                            JSONObject listOne = JSONObjectUtils.get(list, i);
                            BuMerchantCouponDto buMerchantCouponDto = convertBuMerchantCouponDto(listOne, data);
                            buMerchantCouponDtoList.add(buMerchantCouponDto);
                        }
                    }
                }
            }
        }
        return buMerchantCouponDtoList;
    }


    private static BuMerchantCouponDto convertBuMerchantCouponDto(JSONObject listOne, JsonObject data) {
        BuMerchantCouponDto buMerchantCouponDto = new BuMerchantCouponDto();

        buMerchantCouponDto.id = listOne.getLongValue("id");
        buMerchantCouponDto.invalid = listOne.getIntValue("invalid");
        buMerchantCouponDto.merchantId = listOne.getLongValue("merchantId");
        if (buMerchantCouponDto.merchantId > 0) {
            JSONObject sysAdmin = JSONObjectUtils.get(data, "sys_admin");
            buMerchantCouponDto.merchantName = JSONObjectUtils.getString(sysAdmin, buMerchantCouponDto.merchantId.toString(), "name");
        }
        buMerchantCouponDto.couponId = listOne.getLongValue("couponId");
        buMerchantCouponDto.createTime = listOne.getLongValue("createTime");
        if (buMerchantCouponDto.couponId > 0) {
            JSONObject buCoupon = JSONObjectUtils.get(data, "bu_coupon");
            JSONObject buCouponDto = JSONObjectUtils.getJSONObject(buCoupon, buMerchantCouponDto.couponId.toString());
            buMerchantCouponDto.couponName = buCouponDto.getString("name");
            buMerchantCouponDto.dispatchCount = buCouponDto.getIntValue("dispatchCount");
            buMerchantCouponDto.donateCount = buCouponDto.getIntValue("donateCount");
            buMerchantCouponDto.usedCount = buCouponDto.getIntValue("usedCount");
            buMerchantCouponDto.remainCount = buCouponDto.getIntValue("remainCount");
        }
        return buMerchantCouponDto;
    }
}

